package lt.marsrutai.mobile.android.map.overlay;

import java.util.HashSet;
import java.util.List;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.model.Schedule;
import lt.marsrutai.mobile.android.model.Stop;
import lt.marsrutai.mobile.android.util.UnitConversionUtils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StopBalloonView extends BalloonView<StopOverlayItem> {
    
    public StopBalloonView(Context context) {
        super(context);
    }
    
    public StopBalloonView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public void setData(StopOverlayItem item) {
        super.setData(item);
        
        Stop stop = item.getStop();
        List<Schedule> schedules = stop.getSchedules();
        
        if (schedules != null) {
            ViewGroup schedulesLayout = (ViewGroup) findViewById(R.id.schedules);
            schedulesLayout.removeAllViews();
            
            LayoutInflater inflater = LayoutInflater.from(getContext());
            
            // There might be multiple schedules with the same name due to upcoming
            // schedule changes. Therefore, we should filter the unique names only with the help of hash set.
            HashSet<String> scheduleNames = new HashSet<String>();
            
            for (Schedule schedule : schedules) {
                TextView scheduleNumber = (TextView) inflater.inflate(R.layout.schedule_number_small, null);
                
                String scheduleName = schedule.getName();
                if (scheduleNames.add(scheduleName)) {
                    scheduleNumber.setText(scheduleName);
                    scheduleNumber.setBackgroundColor(AppContext.getTransportInfoResolver().resolveTransportColor(
                            item.getScopeFolder(),
                            schedule.getTransportId()));

                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
                    layoutParams.setMargins(0, 0, UnitConversionUtils.dpToPx(getContext(), 5), 0);
                    schedulesLayout.addView(scheduleNumber, layoutParams);
                }
            }
        }
    }

    protected int getLayout() {
        return R.layout.stop_details_view;
    }
}
